package com.example.fmodtest;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.fmodtest.databinding.ActivityMainBinding;

import org.fmod.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'fmodtest' library on application startup.
    static {
        System.loadLibrary("fmod");
        System.loadLibrary("fmodL");
        System.loadLibrary("fmodtest");
    }

    private ActivityMainBinding binding;
    private ExecutorService fixedThreadPool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Log.d("hello","fuck!");
        // Example of a call to a native method
        TextView tv = binding.sampleText;
        tv.setText("stringFromJNI()");

        String path="file:///android_asset/owl.mp3";
        String savePath=this.getApplicationContext().getFilesDir().getPath()+"/therecodr.wav";

        Button btn=findViewById(R.id.button5);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("hello","button 1 clicked");
                dosth(path,savePath,5);
            }
        });

        Button btn2=findViewById(R.id.button6);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("hello","button 2 clicked");
                dosth(path,savePath,3);
            }
        });

        Button btn3=findViewById(R.id.button8);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("hello","button 4 clicked");
                dosth(path,savePath,4);
            }
        });

        FMOD.init(this);
        fixedThreadPool = Executors.newFixedThreadPool(1);
    }

    private void dosth(String path,String savePath,int type){

        new Thread(()->{

            checkFile(savePath);

            //int j=doVoiceEffect(path,savePath,type);
            //Log.d("hello","录制完毕:j:"+j);
            /*
            try {
                createText();
            } catch (IOException e) {
                e.printStackTrace();
            }
             */
        }).start();
    }

    private void checkFile(String savePath){
        File file = new File(savePath);
        if (file.exists()){
            Log.d("hello","文件已存在！");
            paySound(savePath,1);
        }else{
            Log.d("hello","无！");
        }
    }

    public void createText() throws IOException {
        String path=this.getApplicationContext().getFilesDir().getPath();
        File dir = new File(path+"/my_jack2.txt");
        if (!dir.exists()) {
            try {
                //在指定的文件夹中创建文件
                dir.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.d("hello","创建新文件");
        }else{
            Log.d("hello","已经有了");
            readFile(dir);
        }
        Log.d("hello",path);
    }

    @Override
    protected void onDestroy() {
        FMOD.close();
        super.onDestroy();
    }

    private void readFile(File file) throws IOException {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            bufferedReader.close();
            Log.d("hello","content:"+stringBuilder.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private void writeFile(File file) throws IOException {
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            String str = "this is some content";
            outputStream.write(str.getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * A native method that is implemented by the 'fmodtest' native library,
     * which is packaged with this application.
     */
    //public native String stringFromJNI();
    public native int doVoiceEffect(String filePath,String savePath,int mode);
    public native void paySound(String filePath,int mode);
}